/*
 * MIT License
 *
 * Copyright (c) 2025 milkpotatoes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

export { };

const repeatStr = (str: string, size: number) => {
    let result = '';
    for (let i = 0; i < size; i++) {
        result += str;
    }
    return result;
}

const timerList: Map<string, bigint> = new Map();

const NS_TO_MS = 10n ** 6n;
const MIN_NS_HEADER_LEN = String(NS_TO_MS).length;

const innerTimeInNs: () => bigint = exchangeInnerMethod(console, "_innerTimeInNs");

function time(label: any = 'default') {
    if (typeof label != "string") {
        label = String(label);
    }

    if (timerList.has(label)) {
        console.warn(`Timer '${label}' already exists`);
        return;
    }

    const start = innerTimeInNs();
    timerList.set(label, start);
}

function timeEnd(label: string = 'default') {
    const end = innerTimeInNs();
    if (typeof label != "string") {
        label = String(label);
    }

    const start = timerList.get(label)!;
    if (start === void 0) {
        console.warn(`Timer '${label}' does not exist`);
        return;
    }

    let cost = end - start;
    let ms = cost / NS_TO_MS;
    const needFill = MIN_NS_HEADER_LEN - String(cost).length;
    while (cost % 10n === 0n) cost /= 10n;
    console.info(`${label}: ${ms}.${repeatStr('0', needFill)}${String(cost)} ms`);
    timerList.delete(label);
}

function timeLog(label: string = 'default', ...data: any[]) {
    const end = innerTimeInNs();
    if (typeof label != "string") {
        label = String(label);
    }

    const start = timerList.get(label)!;
    if (start === void 0) {
        console.warn(`Timer '${label}' does not exist`);
        return;
    }

    let cost = end - start;
    let ms = cost / NS_TO_MS;
    const needFill = MIN_NS_HEADER_LEN - String(cost).length;
    while (cost % 10n === 0n) cost /= 10n;
    console.info(`${label}: ${ms}.${repeatStr('0', needFill)}${String(cost)} ms`, ...data);
}

function assert(arg: boolean, ...msg: any[]) {
    if (!arg) {
        if (msg.length > 0) {
            console.error('Assertion Failed:', ...msg);
        } else {
            console.error('Assertion Failed');
        }
    }
}

exportsToObject(console, [
    ['time', time],
    ['timeEnd', timeEnd],
    ['timeLog', timeLog],
    ['assert', assert],
])